import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
// import ElementPlus from 'element-plus'
// import 'element-plus/dist/index.css'
import './assets/less/index.less'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
//引入Elmessage和Elloading的css样式文件
import 'element-plus/theme-chalk/el-loading.css'
import 'element-plus/theme-chalk/el-message.css'

import './api/mock.js'
import { createPinia } from 'pinia'
import api from './api/api'
import { useAsideMenu, useToken, useRouting } from "./storePiana/index"
import Cookies from 'js-cookie'
const app = createApp(App)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}
// app.use(ElementPlus)
app.config.globalProperties.$api = api
app.use(createPinia())
//pinia要在app.use之后才能用

//从localStorage获取数据，防止页面刷新数据丢失
const AsideMenu = useAsideMenu()
//动态路由
const Routing = useRouting()
AsideMenu.addMenu(router)
Routing.routing(router)
app.use(router)

//路由守卫
function checkRouter(path) {
  let hasCheck = router.getRoutes().filter(route => route.path == path).length
  if (hasCheck) {
    return true
  } else {
    return false
  }

}
checkRouter()

// const Token = useToken()
router.beforeEach((to, from, next) => {
  // Token.getToken()
  const token = Cookies.get('token')
  // console.log('Token.token', token)
  if (!token && to.name !== 'login') {
    next({ name: 'login' })
  } else if (!checkRouter(to.path)) {
    next({ name: 'home' })
  }
  else {
    next()
  }
})
app.mount('#app')


